//
//  JYShopCartBottomView.m
//  JYFarm
//
//  Created by 花花 on 2017/10/21.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYShopCartBottomView.h"


@interface JYShopCartBottomView()

@property(nonatomic)UIButton *allSelectBtn;//全选


@property (nonatomic, strong) UILabel *totalPriceLablel; //合计价格


@property (nonatomic, strong) UILabel *depositPriceLablel; //合计价格

@property(nonatomic)UIButton *commitOrderBtn;//提交订单

@property (nonatomic, strong) UIView *separateLine; //分割线
@end

@implementation JYShopCartBottomView

#pragma mark - init Method
- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
    
        [self setupSubView];
    }
    return self;
}
- (void)configureShopcartBottomViewWithTotalPrice:(double)totalPrice DepositPrice:(double)depositPrice totalCount:(double)totalCount isAllselected:(BOOL)isAllSelected{
    
    self.allSelectBtn.selected = isAllSelected;
    self.totalPriceLablel.text = [NSString stringWithFormat:@"合 计:￥%.2f元", totalPrice + depositPrice];
    self.depositPriceLablel.text = [NSString stringWithFormat:@"含押金:￥%.2f元", depositPrice];
    [self renderWithTotalPrice:[NSString stringWithFormat:@"￥%.2f元", totalPrice + depositPrice] Label:self.totalPriceLablel];
    [self renderWithTotalPrice:[NSString stringWithFormat:@"￥%.2f元", depositPrice] Label:self.depositPriceLablel];
    //[NSString stringWithFormat:@"提交订单(%ld)", totalCount]
    [self.commitOrderBtn setTitle:@"提交订单" forState:UIControlStateNormal];
//    self.commitOrderBtn.enabled = totalCount && totalPrice;
//    if (self.commitOrderBtn.isEnabled) {
        [self.commitOrderBtn setBackgroundColor:kThemeColor];

//    } else {
//        [self.commitOrderBtn setBackgroundColor:[UIColor lightGrayColor]];

//    }

}

#pragma mark - layoutSubviews
- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.allSelectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(10);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
    
    [self.commitOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self);
        make.width.equalTo(@120);
    }];
    
    [self.totalPriceLablel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.equalTo(self.allSelectBtn.mas_right).offset(5);
        make.height.mas_equalTo(16);
        make.right.mas_equalTo(self.commitOrderBtn.mas_left).offset(-5);
        

    }];
    
    [self.depositPriceLablel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30);
        make.left.equalTo(self.allSelectBtn.mas_right).offset(5);
        make.height.mas_equalTo(16);
        make.right.mas_equalTo(self.commitOrderBtn.mas_left).offset(-5);


    }];
    [self.separateLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.equalTo(@0.5);
    }];
}

#pragma mark - Cutom Method
- (void)renderWithTotalPrice:(NSString *)totalPrice Label:(UILabel *)label{

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 4;
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text attributes:@{NSParagraphStyleAttributeName:paragraphStyle}];
    
    [attributedString addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:210/255.0 green:50/255.0 blue:50/255.0 alpha:1]} range:[label.text rangeOfString:totalPrice]];
    
    label.attributedText = attributedString;
    
    label.textAlignment = NSTextAlignmentLeft;
}
#pragma mark - setupSubView
- (void)setupSubView{
    [self addSubview:self.allSelectBtn];
    [self addSubview:self.totalPriceLablel];
    [self addSubview:self.depositPriceLablel];
    [self renderWithTotalPrice:@"￥0.00元" Label:self.totalPriceLablel];
    [self renderWithTotalPrice:@"￥0.00元" Label:self.depositPriceLablel];
    [self addSubview:self.commitOrderBtn];
    
    [self addSubview:self.separateLine];
    
    
}
#pragma mark - Setter && Getter Methods
- (UIButton *)allSelectBtn {
    if (_allSelectBtn == nil){
        _allSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_allSelectBtn setTitle:@"全选" forState:UIControlStateNormal];
        [_allSelectBtn setTitleColor:[UIColor colorWithRed:150/255.0 green:150/255.0 blue:150/255.0 alpha:1] forState:UIControlStateNormal];
        _allSelectBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_allSelectBtn setImage:[UIImage imageNamed:@"shopping_cart_select"] forState:UIControlStateNormal];
        [_allSelectBtn setImage:[UIImage imageNamed:@"shopping_cart_select_click"] forState:UIControlStateSelected];
        [_allSelectBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
        [_allSelectBtn addTarget:self action:@selector(allSelectButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _allSelectBtn;
}
- (UILabel *)totalPriceLablel {
    if (_totalPriceLablel == nil){
        _totalPriceLablel = [[UILabel alloc] init];
        _totalPriceLablel.textAlignment = NSTextAlignmentLeft;
        _totalPriceLablel.font = [UIFont systemFontOfSize:14];
        _totalPriceLablel.textColor = [UIColor colorWithRed:120/255.0 green:120/255.0 blue:120/255.0 alpha:1];
        _totalPriceLablel.text = @"合 计:￥0.00元";
    }
    return _totalPriceLablel;
}

- (UILabel *)depositPriceLablel{
    
    if (_depositPriceLablel == nil) {
        
        _depositPriceLablel = [[UILabel alloc] init];
        _depositPriceLablel.font = [UIFont systemFontOfSize:14];
        _depositPriceLablel.textColor = [UIColor colorWithRed:120/255.0 green:120/255.0 blue:120/255.0 alpha:1];
        _depositPriceLablel.text = @"含押金：￥0.00元";
    }
    
    return _depositPriceLablel;
}
- (UIView *)separateLine {
    if (_separateLine == nil){
        _separateLine = [[UIView alloc] init];
        _separateLine.backgroundColor = kf2Color;
    }
    return _separateLine;
}
- (UIButton *)commitOrderBtn {
    if (_commitOrderBtn == nil){
        _commitOrderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_commitOrderBtn setTitle:@"提交订单" forState:UIControlStateNormal];
        [_commitOrderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _commitOrderBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_commitOrderBtn setBackgroundColor:kThemeColor];
        [_commitOrderBtn addTarget:self action:@selector(commitOrderButtonAction) forControlEvents:UIControlEventTouchUpInside];
        //        _commitOrderBtn.enabled = NO;
    }
    return _commitOrderBtn;
}

#pragma mark - SEL Methods
- (void)allSelectButtonAction {
    self.allSelectBtn.selected = !self.allSelectBtn.isSelected;
    
    if (self.HandlerAllBtnOnClick) {
        self.HandlerAllBtnOnClick(self.allSelectBtn.isSelected);
    }
}
- (void)commitOrderButtonAction{
    
    if (_HandlerCommitOrderBtnOnClick) {
        _HandlerCommitOrderBtnOnClick();
    }

}

@end



#pragma mark --------------------------JYShopCartTopView----------------------------
@interface JYShopCartTopView()

@property(nonatomic)UIButton *deleteBtn;//批量删除

@property(nonatomic)UIButton *emptyBtn;//清空
@end


@implementation JYShopCartTopView
#pragma mark - init Methods
- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupSubView];
    }
    return self;
}
#pragma mark - setupSubView
- (void)setupSubView{

    [self addSubview:self.deleteBtn];
    
    [self addSubview:self.emptyBtn];
}

#pragma mark - layoutSubviews
- (void)layoutSubviews{

    [super layoutSubviews];
    
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.height.mas_equalTo(self.mas_height);
        make.width.mas_equalTo(100);
    }];
    
    [self.emptyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.height.mas_equalTo(self.mas_height);
        make.width.mas_equalTo(100);
    }];
}
#pragma mark - Setter && Getter Methods
- (UIButton *)deleteBtn{
    if (!_deleteBtn) {
        _deleteBtn = [self btnImgName:@"shopping_cart_batch" title:@"批量删除" tag:ShopBtnType_delete];
        [_deleteBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    }
    return _deleteBtn;
    
}
- (UIButton *)emptyBtn{
    
    if (!_emptyBtn) {
        _emptyBtn = [self btnImgName:@"shopping_cart_deleting" title:@"清空" tag:ShopBtnType_empty];
    }
    return _emptyBtn;
    
}
- (UIButton *)btnImgName:(NSString *)imgName title:(NSString *)title tag:(NSInteger)tag{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = kFont(13);
    btn.tag =tag;
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:k3Color forState:UIControlStateNormal];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -5)];
    
    return btn;
}

#pragma mark - SEL Methods
- (void)btnOnClick:(UIButton *)btn{
    
    if (btn.tag == ShopBtnType_delete) {
        
        if(_HandlerDeleteBtnOnClick){
        
            _HandlerDeleteBtnOnClick();
        }
    }else if(btn.tag == ShopBtnType_empty){
        
        if(_HandlerEmptyBtnOnClick){
            
            _HandlerEmptyBtnOnClick();
        }
    }
    
}
@end



